package com.eastfair.notice.listener;

import com.alibaba.fastjson.JSON;
import com.eastfair.notice.dto.MsgSendDTO;
import com.eastfair.annotation.base.LanguageTypeEnum;
import com.eastfair.enums.MsgTypeEnum;
import com.eastfair.notice.dto.MsgMqDTO;
import com.eastfair.notice.entity.MsgLog;
import com.eastfair.notice.service.MsgLogService;
import com.eastfair.projectcore.entity.MsgTemplate;
import com.eastfair.third.im.dto.ImConfDTO;
import com.eastfair.third.im.dto.ImMsgBodyDTO;
import com.eastfair.third.im.dto.ImMsgContentTextDTO;
import com.eastfair.third.im.dto.ImMsgDTO;
import com.eastfair.third.im.enums.ImMsgTypeEnum;
import com.eastfair.third.im.enums.ImUrlEnum;
import com.eastfair.third.im.utils.ImUtil;
import com.eastfair.third.model.MessageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import java.util.Collections;

/**
 * 腾讯im发送mq消费
 * @ClassName NoticeSendListener
 * @Description TODO
 * @Author liuxiaobai
 * @Date 2021/8/9 2:05 下午
 * @Version 1.0
 */
@Component
public class ImSendListener {
    private static final Logger logger = LoggerFactory.getLogger(ImSendListener.class);

    @Autowired
    private MsgLogService msgLogService;

    @JmsListener(destination = "${activemq.queue.imSend}")
    public void handleMessage(String json) {
        try {
            MsgMqDTO msg = JSON.parseObject(json,MsgMqDTO.class);
            ImConfDTO conf = msg.getImConfig();
            MsgSendDTO sendDTO = msg.getMsgSendDTO();
            MsgTemplate template = msg.getTemplate();
            if (conf == null || sendDTO == null || template == null) {
                return;
            }
            //根据语言取对应消息内容
            String content = LanguageTypeEnum.CN.equals(sendDTO.getLanguageType()) ? template.getContent() : template.getEnContent();
            //构建im消息类参数
            ImMsgDTO imMsgDTO = ImMsgDTO.builder()
                    .toAccount(Collections.singletonList(sendDTO.getReceiveUserId().toString()))
                    .imMsgBodyDTO(Collections.singletonList(ImMsgBodyDTO.builder()
                            .msgType(ImMsgTypeEnum.TEXT.getMsgType())
                            .msgContent(ImMsgContentTextDTO.builder().text(content).build())
                            .build()))
                    .msgRandom(ImUtil.getRandom())
                    .build();
            //调用im组件发出
            MessageResult result = ImUtil.send(conf,imMsgDTO, ImUrlEnum.SEND_MSG_BATCH);
            //加入日志
            msgLogService.save(MsgLog.builder()
                    .projectId(sendDTO.getProjectId())
                    .exhibitionId(sendDTO.getExhibitionId())
                    .type(MsgTypeEnum.IM.name())
                    .tempId(template.getId())
                    .receiveType(sendDTO.getReceiveType().name())
                    .receiveId(sendDTO.getReceiveUserId())
                    .receive(sendDTO.getReceiveUserId().toString())
                    .content(content)
                    .status(result.getStatus())
                    .errMsg(result.getCode() != null ? result.getCode() + result.getErrorMsg() : result.getErrorMsg())
                    .build());
        } catch (Exception e) {
            logger.error("im消费",e);
        }
    }

}
